﻿using System;
using System.Windows;
using System.Windows.Input;
using MyFloridaHouse.libSvOpenDistricts.MapShapes;
using MyFloridaHouse.libSvOpenDistricts.ViewModel;

namespace MyFloridaHouse.SilverlightOpenDistricts
{

    public partial class MainPage
    {

        public void InitializeEvents()
        {
            InitializeNotifyUserEvent();
            InitializeDistrictDataSummaryEvents();
            InitializeRegionEvents();
            //InitializeMapShapeLayerEvents();
            //InitializeGeocodeEvents();

        }


        // this is a VIEW trigger that listens for an event in the VIEW-MODEL
        public void InitializeNotifyUserEvent()
        {
            ViewModelBase.NotifyUser += onNotifyUser;
        }

        public void onNotifyUser(NotifyUserEventHandlerArgs args)
        {
            string message = args.message;
            if (args.ex != null) message += " " + args.ex.Message;
            MessageBox.Show(message);
        }
        // End VIEW trigger


        // Region
        //
        public void InitializeRegionEvents()
        {
            Region.RegionMouseEnter += new RegionMouseEnterEventHandler(RegionMouseEnterHandler);
            Region.RegionMouseLeave += new RegionMouseLeaveEventHander(RegionMouseLeaveHandler);
            Region.RegionMouseLeftButtonUp += new RegionMouseLeftButtonUpEventHandler(RegionMouseLeftButtonUpHandler);
        }

        public void RegionMouseLeaveHandler(object sender, EventArgs e)
        {
            // no action required
        }


        public void RegionMouseEnterHandler(object sender, EventArgs e)
        {
            Region region = ((Region)sender);

            if (region.layer.layerName == "District")
            {
                viewModelBase.districtPlan.currentDistrict = region.regionID.ToString();
            }
            else
            {
                // can only find if region is specifically in condensed list
                viewModelBase.districtPlan.currentDistrict = viewModelBase.districtPlan.GetDistrictFromRegionID(region.layer.layerID, region.regionID);
            }

            if (!viewModelBase.isFreezeData)
            {
                //if (viewModelBase.regionRetrievingCount == 0)
                //{
                    serviceDAL.GetRegionData(region.layer.layerID, region.regionID);
                //}
            }

        }

        public void RegionMouseLeftButtonUpHandler(object sender, MouseButtonEventArgs e)
        {
            Region rgn = (Region)sender;
            //MessageBox.Show("Click");
            //TODO: if this isn’t needed to freeze region data, then remove this method

        }
        
        // Geocode
        //
        //public void InitializeGeocodeEvents()
        //{
        //    xGeocodeResults.GeocodeSelected += new GeocodeSelectedEventHandler(onGeocodeSelected);
        //}

        //public void onGeocodeSelected(object sender, GeocodeSelectedEventArgs e)
        //{
        //    // Only report on first result.
        //    Location loc = GeocodeLayer.AddResult(e.geocodeResult);
        //    // Zoom the map to the location of the item.
        //    MyXamlMainMap.SetView(loc, GEOCODE_ZOOM_LEVEL);
        //}

    }
}
